/**************************************************************************//**
 * @file           main.c
 * @version        V1.0
 * $Revision:      1 $
 * $Date:          2017/01/04 $
 * @brief          Main function
 * @Author         Jeffrey Kang
 * @ContactInfo    chkang@esmt.com.tw
 *
 * @note
 *
 * Copyright (C) 1998 Elite Semiconductor Memory Technology Corp.
 * All rights reserved.
 *
 ******************************************************************************/


#include <REG52.h>
#include "system_FE81001.h"


static volatile unsigned char xdata cap0_cnt[2];
static volatile unsigned char xdata cap1_cnt[2];
static volatile unsigned char xdata cap2_cnt[2];
static volatile unsigned char xdata cap3_cnt[2];


/**
 * @brief       delay function
 *
 * @param       None
 *
 * @return      None
 *
 * @details     1. 
 */
void delay( void )
{
	unsigned int i;
	
	for( i = 0 ; i < 1000 ; i++ ){}
}


/**
 * @brief       EINT0ISR function
 *
 * @param       None
 *
 * @return      None
 *
 * @details     1. 
 */
const void CAPISR (void) interrupt ISR_CAP
{
	/* read for clear interrupt flag */
	unsigned char int_stat = CAP->capintf;
	
	/* capture 0 interrupt */
	if ((int_stat & 0x01) == 0x01){
		cap0_cnt[0] = CAP->cap0_cnt[0];
		cap0_cnt[1] = CAP->cap0_cnt[1];
	}
	
	/* capture 1 interrupt */
	if ((int_stat & 0x04) == 0x04){
		cap1_cnt[0] = CAP->cap1_cnt[0];
		cap1_cnt[1] = CAP->cap1_cnt[1];
	}
	
	/* capture 2 interrupt */
	if ((int_stat & 0x10) == 0x10){
		cap2_cnt[0] = CAP->cap2_cnt[0];
		cap2_cnt[1] = CAP->cap2_cnt[1];
	}
	
	/* capture 3 interrupt */
	if ((int_stat & 0x40) == 0x40){
		cap3_cnt[0] = CAP->cap3_cnt[0];
		cap3_cnt[1] = CAP->cap3_cnt[1];
	}
	
	/* enable global interrupt */
	INTC->int_gmask = 0x01;
}


/**
 * @brief       EINT3Init function
 *
 * @param       None
 *
 * @return      None
 *
 * @details     1. 
 */
void CAP3Init(void)
{
	/* enable PD.3 input */
	PD->gpio_ms[0] &= 0xF7;
	PD->gpio_ms[1] &= 0xF7;
	PD->gpio_ms[2] &= 0xF7;
	
	/* enable PD.3 CAP function */
	PD->gpio_ctrl[0] |= 0x08;
	PD->gpio_ctrl[1] &= 0xF7;
	
	/* capture config */
	CAP->prescale = 0;	//	PCLK / (PRESCALE + 1)
	Set_CAP3_LPF_1();
	Set_CAP3_Mode_Edge();
	Enable_Cap3_interrupt();
	Enable_CAP3();
}


/**
 * @brief       EINT2Init function
 *
 * @param       None
 *
 * @return      None
 *
 * @details     1. 
 */
void CAP2Init(void)
{
	/* enable PD.2 input */
	PD->gpio_ms[0] &= 0xFB;
	PD->gpio_ms[1] &= 0xFB;
	PD->gpio_ms[2] &= 0xFB;
	
	/* enable PD.2 CAP function */
	PD->gpio_ctrl[0] |= 0x04;
	PD->gpio_ctrl[1] &= 0xFB;
	
	/* capture config */
	CAP->prescale = 0;	//	PCLK / (PRESCALE + 1)
	Set_CAP2_LPF_1();
	Set_CAP2_Mode_Edge();
	Enable_Cap2_interrupt();
	Enable_CAP2();
}


/**
 * @brief       EINT1Init function
 *
 * @param       None
 *
 * @return      None
 *
 * @details     1. 
 */
void CAP1Init(void)
{
	/* enable PD.1 input */
	PD->gpio_ms[0] &= 0xFD;
	PD->gpio_ms[1] &= 0xFD;
	PD->gpio_ms[2] &= 0xFD;
	
	/* enable PD.1 CAP function */
	PD->gpio_ctrl[0] |= 0x02;
	PD->gpio_ctrl[1] &= 0xFD;
	
	/* capture config */
	CAP->prescale = 0;	//	PCLK / (PRESCALE + 1)
	Set_CAP1_LPF_1();
	Set_CAP1_Mode_Edge();
	Enable_Cap1_interrupt();
	Enable_CAP1();
}


/**
 * @brief       EINT0Init function
 *
 * @param       None
 *
 * @return      None
 *
 * @details     1. 
 */
void CAP0Init(void)
{
	/* enable PD.0 input */
	PORT_SetPDInput(BIT0);
	
	/* enable PD.0 CAP function */	
	Set_PD0_CAP0();
	
	/* capture config */
	CAP->prescale = 0;	//	PCLK / (PRESCALE + 1)
	Set_CAP0_LPF_1();
	Set_CAP0_Mode_Edge();
	Enable_Cap0_interrupt();
	Enable_CAP0();
}


/**
 * @brief       main function
 *
 * @param       None
 *
 * @return      None
 *
 * @details     1. 
 */
int main( void )
{
	/* enable GPIO clock */
	PORT_PAPBPCPD_Clock_Enable();		// enable GPIOA GPIOB GPIOC GPIOD clock
	
	/* enable capture clock */
	En_CAP_clk();		// external capture clock
	
	/* enable cap0 function */
	CAP0Init();
	
	/* enable cap1 function */
	CAP1Init();
	
	/* enable cap2 function */
	CAP2Init();
	
	/* enable cap3 function */
	CAP3Init();
	
	/* enable global interrupt */
	INTC->int_gmask = 0x01;
	
	while(1)
	{
		
	}
}

